package com.person.pay.service.impl;

import com.lly835.bestpay.enums.BestPayPlatformEnum;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.enums.OrderStatusEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.service.BestPayService;
import com.person.pay.dao.PayInfoMapper;
import com.person.pay.enums.PayPlatformEnum;
import com.person.pay.pojo.PayInfo;
import com.person.pay.service.IpayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author 最酷的桃儿wjc
 * * @date 2022/5/8
 */
@Slf4j
@Service
public class PayService implements IpayService {
    @Autowired
    private BestPayService bestPayService;

    @Autowired
    private PayInfoMapper payInfoMapper;

    @Override
    public PayResponse create(String orderId, BigDecimal amount,BestPayTypeEnum bestPayTypeEnum) {

        PayInfo payInfo = new PayInfo(Long.parseLong(orderId), PayPlatformEnum.getBestPayTypeEnum(bestPayTypeEnum).getCode(), OrderStatusEnum.NOTPAY.name(),amount);

        payInfoMapper.insertSelective(payInfo);

        PayRequest payRequest = new PayRequest();
        //创建订单名称
        payRequest.setOrderName("4283388-魏靖程测试订单");
        //创建订单号码
        payRequest.setOrderId(orderId);
        //创建订单金额
        payRequest.setOrderAmount(amount.doubleValue());
        //创建支付场景
        payRequest.setPayTypeEnum(bestPayTypeEnum);
        //发起支付
        PayResponse response = bestPayService.pay(payRequest);
        //日志输出
        log.info("response={}",response);
        //返回响应对象
        return response;
    }

    @Override
    public String asyncNotify(String notifyData) {
        //1.签名校验
        PayResponse payResponse = bestPayService.asyncNotify(notifyData);
        //2.通过orderNo查询数据库中的数据
        PayInfo payInfo = payInfoMapper.selectByOrderNo(Long.parseLong(payResponse.getOrderId()));
        if (payInfo == null){
            throw new RuntimeException("通过orderNo查询到的结果是null");
        }
        //如果订单支付状态不是已支付
        if (!payInfo.getPlatformStatus().equals(OrderStatusEnum.SUCCESS.name())){
            //就进行支付金额的比较
            if (payInfo.getPayAmount().compareTo(BigDecimal.valueOf(payResponse.getOrderAmount())) != 0){
                throw new RuntimeException("异步通知中的金额和数据库里不一致，orderNo" + payResponse.getOrderId());
            }
            payInfo.setPlatformStatus(OrderStatusEnum.SUCCESS.name());
            payInfo.setPlatformNumber(payResponse.getOutTradeNo());
            payInfo.setUpdateTime(null);
            payInfoMapper.updateByPrimaryKeySelective(payInfo);
        }
        //4.返回通知给微信或者支付宝
        if(payResponse.getPayPlatformEnum() == BestPayPlatformEnum.WX){
            return "<xml>\n" +
                    "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                    "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                    "</xml>";
        } else if (payResponse.getPayPlatformEnum() == BestPayPlatformEnum.ALIPAY){
            return "success";
        }

        log.info("payResponse={}",payResponse);
        throw new RuntimeException("异步通知中");
    }

    @Override
    public PayInfo queryByOrderNo(String orderNo) {
        return payInfoMapper.selectByOrderNo(Long.parseLong(orderNo));
    }
}
